package com.jiayang.hhotel.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.jiayang.hhotel.R;
import com.jiayang.hhotel.bean.RoundsModel;
import com.jiayang.hhotel.util.StringUtil;
import com.yanzhenjie.recyclerview.swipe.SwipeMenuAdapter;

import java.util.List;

/**
 * Created by Administrator on 2017/5/16 0016.
 * 查房列表适配器
 */

public class RoundsListAdapter extends SwipeMenuAdapter<RoundsListAdapter.ViewHolder> {

//    private static final int TYPE_ITEM =0; //普通Item View
//    private static final int TYPE_FOOTER = 1; // 底部FootView

    private List<RoundsModel.PCheckRoomListDtosBean> mList;
    private Context t;
    private boolean isShowBt=true;//是否显示按钮

    private btClickListener listener;

    /**
     * 构造方法
     * @param context 上下文
     * @param list 集合
     * @param is  是否显示按钮
     * @param page 页数，用于控制集合的数据是否叠加
     * @param listener 监听
     */
    public RoundsListAdapter(Context context, List<RoundsModel.PCheckRoomListDtosBean> list, boolean is,int page, btClickListener listener){
        if(page == 1){
            this.mList = list;
        }else{
            this.mList.addAll(list);
        }
        this.isShowBt = is;
        this.t = context;
        this.listener = listener;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        RoundsModel.PCheckRoomListDtosBean model = mList.get(position);
        char[] state=model.getStatus().toCharArray();
        String stateText="";
        if(String.valueOf(state[0]).equals("0")){//未完成
            stateText+=t.getString(R.string.not_finish)+t.getString(R.string.space);
        }else{
            stateText+=t.getString(R.string.already_finish)+t.getString(R.string.space);
        }
        if(String.valueOf(state[1]).equals("0")){//0-未归，1-已归
            stateText+=t.getString(R.string.did_not_return)+t.getString(R.string.space);
        }else{
            stateText+=t.getString(R.string.did_return)+t.getString(R.string.space);
        }
        if(String.valueOf(state[2]).equals("0")){//0-客房物品没有损坏，1-客房物品有损坏
            stateText+=t.getString(R.string.thing_not_damaged);
        }else{
            stateText+=t.getString(R.string.thing_damaged);
        }
        holder.rmType.setText(stateText);
        holder.rmNum.setText(t.getString(R.string.room_num)+t.getString(R.string.space)+model.getRoom());
        holder.rmName.setText(t.getString(R.string.room_name)+t.getString(R.string.space)+model.getAcctname());
        holder.psCount.setText(t.getString(R.string.person_count)+t.getString(R.string.space)+model.getAdults());
        holder.applyTime.setText(Html.fromHtml(StringUtil.getDiffColorText(t.getString(R.string.apply))+t.getString(R.string.space)+model.getOrgTime()));
        holder.dispatching.setText(Html.fromHtml(StringUtil.getDiffColorText(t.getString(R.string.dispatching))+t.getString(R.string.space)+model.getPatchTime()));
        holder.roundsTime.setText(Html.fromHtml(StringUtil.getDiffColorText(t.getString(R.string.rounds))+t.getString(R.string.space)+model.getCheckTime()));
        if(isShowBt){
            holder.btParent.setVisibility(View.VISIBLE);
            holder.bind(model,position);
        }else{
            holder.btParent.setVisibility(View.GONE);
        }
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    @Override
    public View onCreateContentView(ViewGroup parent, int viewType) {
        return LayoutInflater.from(t).inflate(R.layout.rounds_list_item, parent, false);
    }

    @Override
    public ViewHolder onCompatCreateViewHolder(View realContentView, int viewType) {
        return new ViewHolder(realContentView);
    }

    class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        TextView rmType;//房间类型
        TextView rmNum;//房间编号
        TextView rmName;//房间名称
        TextView psCount;//人数
        TextView applyTime;//申请时间
        TextView dispatching;//派工时间
        TextView roundsTime;//查房时间

        Button start;//开始
        Button finish;//完成
        Button thing;//客房物品

        LinearLayout btParent;//按钮父布局

        public ViewHolder(View convertView) {
            super(convertView);
            rmType= (TextView) convertView.findViewById(R.id.tv_room_type);
            rmNum= (TextView) convertView.findViewById(R.id.tv_room_id);
            rmName= (TextView) convertView.findViewById(R.id.tv_room_name);
            psCount= (TextView) convertView.findViewById(R.id.tv_person_num);
            applyTime= (TextView) convertView.findViewById(R.id.tv_apply);
            dispatching= (TextView) convertView.findViewById(R.id.tv_dispatching);
            roundsTime= (TextView) convertView.findViewById(R.id.tv_rounds);

            start= (Button) convertView.findViewById(R.id.bt_start);
            finish= (Button) convertView.findViewById(R.id.bt_determine);
            thing= (Button) convertView.findViewById(R.id.bt_thing);
            btParent= (LinearLayout) convertView.findViewById(R.id.bt_parent);
        }

        private int position;

        private void bind(RoundsModel.PCheckRoomListDtosBean model,int pos){
            this.position=pos;
            start.setTag(model);
            finish.setTag(model);
            thing.setTag(model);

            start.setOnClickListener(this);
            finish.setOnClickListener(this);
            thing.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            switch (v.getId()){
                case R.id.bt_start:
                    if(listener!=null){
                        listener.onBTClickListener((RoundsModel.PCheckRoomListDtosBean) v.getTag(), R.id.bt_start,position);
                    }
                    break;
                case R.id.bt_determine:
                    if(listener!=null){
                        listener.onBTClickListener((RoundsModel.PCheckRoomListDtosBean) v.getTag(), R.id.bt_determine,position);
                    }
                    break;
                case R.id.bt_thing:
                    if(listener!=null){
                        listener.onBTClickListener((RoundsModel.PCheckRoomListDtosBean) v.getTag(), R.id.bt_thing,position);
                    }
                    break;
            }
        }
    }

    public interface btClickListener{
        void onBTClickListener(RoundsModel.PCheckRoomListDtosBean model, int id, int position);
    }
}
